//
//  UserAccountViewModel.swift
//  Weibo
//
//  Created by 闫博123 on 16/1/18.
//  Copyright © 2016年 闫博123. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {
    
    var userAccount : UserAccount?
    
    override init() {
        
        userAccount = UserAccount.loadUserAccount()
        
        super.init()
    }
    
    // 用户是否登录的属性
    var loagin : Bool
    {
        return UserAccount.loadUserAccount() != nil
    }
    
    // 获取授权Token
    func loadAccessToken(code : String , finished : (isSuccess : Bool) -> ())
    {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = ["client_id" : client_id , "client_secret" : client_secret , "grant_type" : "authorization_code" , "code" : code , "redirect_uri" : redirect_uri]
        
        let AFN = AFHTTPSessionManager()
        
        // 设置支持解析纯文本的JSON格式
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        AFN.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            print(result)
            
            // 类型转换 将AnyObject对象 转换成 [key : value]
            guard let dict = result as? [String : AnyObject] else
            {
                finished(isSuccess: false)
                
                print("数据格式不合法")
                
                return
            }
            
            // 程序走到这里 代表anyObject对象一定被转换为字典格式的对象
            // let access_token = dict["access_token"] as! String
            // let uid = dict["uid"] as! String
            
            let account = UserAccount(dict : dict)
            
            // print(account)
            
            // 加载用户信息
            self.loadUserInfo(account, finished : finished)
            
            }) { (_, error) -> Void in
                
                finished(isSuccess: false)
                
                print(error)
        }
    }
    
    // MARK: 加载用户信息
    private func loadUserInfo(account : UserAccount , finished : (isSuccess : Bool) -> ())
    {
        // 准备url
        let urlString = "https://api.weibo.com/2/users/show.json?"
        
        let parameters = ["access_token" : account.access_token! , "uid" : account.uid!]
        
        // 获取AFN对象
        let AFN = AFHTTPSessionManager()
        
        // 调用GET方法
        AFN.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            // 获取用户数据
            guard let dict = result as? [String : AnyObject] else
            {
                print("数据格式不正确")
                
                finished(isSuccess: false)
                
                return
            }
            
            finished(isSuccess: true)
            
            let name = dict["name"] as! String
            
            let avatar_large = dict["avatar_large"] as! String
            
            account.name = name
            
            account.avatar_large = avatar_large
            
            account.saveAccount()
            
            }) { (_, error) -> Void in
                
                finished(isSuccess: false)
                
                print(error)
        }
    }


}
